/**
 * Foxit Software Inc.
 * Copyright(C) 2005-2014, all rights reserved.
 * The following code is copyrighted and contains proprietary information
 * and trade secrets of Foxit Software Inc.

 * Description: PDFDocument
 * Author: shaojie zhuo
 * Update: shaojie zhuo (2014-05-07 18:13)
 */
define(function (require, exports, module){
    var PDFPage = require('./PDFPage');
    var PDFPageCollection = require('./PDFPageCollection')
    var PDFDocProperties = require('./PDFDocProperties')
    /**
     * PDF Document Class
     * @param {WebPDF.PDFDocType} docType document type
     * @constructor
     */
    var PDFDocument = Backbone.Model.extend({
        initialize: function(){                
            this.pages = new PDFPageCollection();         
            
            var PagesInfo = this.get("PagesInfo");
            for (var i = 0; i < PagesInfo.length; i++) {
               this.pages.add(new PDFPage(PagesInfo[i]));                 
            }
            
            var dppinfo = this.get("dppinfo");            
            if (dppinfo && dppinfo.ppt) {
                 this.docProperties = new PDFDocProperties(dppinfo.ppt);                
            }                
        },     

        /**
         * Get page count of the PDF document.
         * @returns {number} page count of the PDF document.
         */
        getPageCount: function(){
            return this.pages.length;
        },
        /**
         * Get pdf page by index.
         * @param {number} pageIndex index of the page to get.
         * @returns {WebPDF.IPDFPage} page which specified by given index.
         */
        getPage : function(pageIndex){
            if (isNaN(pageIndex))
                return null;
            return this.pages.at(pageIndex);
        },
        
        /**
         * Get type of PDF document
         * @returns {WebPDF.PDFDocType}
         */
        getDocType : function () {
            return this.get("doctype");
        },
        
        /**
         * Get document properties
         * @returns {Object} document properties in JSON format.
         */
        getDocProperties : function () {
            return this.docProperties;
        },
       
        /**
         * delete page from document
         * @param {number} pageIndex index of page to delete
         * @returns {boolean} return true if delete successfully, otherwise  return false.
         * @private
         */
        deletePage : function (pageIndex) {
            if (isNaN(pageIndex) || pageIndex > _pdfPages.length || pageIndex < 0){
                return false;
            }            
            delete this.pages.remove(this.pages.at(pageIndex));
            return true;
        }    
       })
    
    module.exports = PDFDocument;
});